<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Hash;
use Mail;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LoginsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getLogins()
    {
        //
        // echo 'hello';die;
        return view('logs.logins');
    }

    // 登录验证
    public function postChecks(Request $request)
    {
       	// echo 'hello';die;
       	// 查询用户是否存在
       	$user = DB::table('users')->where('email',$request->input('email'))->first();
       	// dd($user);
       	
       	if($user){
       		// 检测密码是否与数据库一致
       		 
       		if(Hash::check($request->input('password'),$user->password)){
       			//dd('hash');
       			//登录成功将用户id放入session
       			session(['id'=>$user->id,'member'=>$user->username]);
            // session(['id'=>$users->id]); 
            // session(['member'=>$users->user]); 

            // dd($user->id);
       			return redirect('/')->with('success','欢迎登录');
       		}else{
       			
       			return back()->with('error','用户名或者密码错误');
       		}
       	}else{
			       return back()->with('error','用户名或者密码错误');
       	}
    }


    // 退出登录
    public function getlogout(){
    	// 清除session中id
    	session()->forget('id');
    	return redirect('/logins');
    }

    //忘记密码
    public function getForget()
    {
      return view('logs.forget');
    }

   
    //发送邮件找回密码

    public function doforget(Request $request)
    {
      // dd('222');
      //读取
      $user =DB::table('users')->where('email',$request->input('email'))->first();
      // dd($user);
      //发送邮件
      Mail::send('email.forget',['user'=>$user],function($message)use($user){
        // $m->from(env('MAIL_USERNAME'), Config::get('app.App_NAME'));

        $message->to($user->email)->subject('找回密码邮件');
      });

      if($user){
        return view('logs.success');
      }
    }
    /**
     * 重置密码表单显示
     */
    public function reset(Request $request)
    {
        //检测 将需要修改的相对应的id和token是否传递过来
        $user=DB::table('users')->where('id',$request->input('id'))->where('token',$request->input('token'))->first();
        // dd($user);
        //解析模板
        return view('logs.reset',['user'=>$user]);
        
    }

    /**
     * 用户更新密码操作方法
     */
    public function doReset(Request $request)
    {
        
        // dd($user);
        $user=DB::table('users')->where('id',$request->input('id'))->where('token',$request->input('token'))->first();
        //信息

        $user->password=Hash::make($request->input('password'));
        $user->token=str_random(50);
        //修改:插入并替换掉原来数据库里的token
        $id = $user->id;
        $new = [];
        foreach ($user as $k => $v) {
            $new[$k] = $v;
        }
        //dd($new);
        $res = DB::table('users')->where('id',$id)->update($new);
        if($res){
            return redirect('/logins')->with('error','密码修改成功请登录');
        }else{
            return back();
        }
    }
}